//SqlProcs.cpp
#include  <windows.h>
//#define   _UNICODE
#include  <tchar.h>
#include  <stdio.h>
#include  <odbcinst.h>  
#include  <sql.h>       
#include  <sqlext.h>    
#include  "Strings.h"
#include  "SqlProcs.h"


unsigned int iInstallerError()
{
 DWORD pErr;
 TCHAR szErrMsg[512];
 WORD  cbMsgBuffer=512;
 WORD  cbRet;
 WORD  wErrNum=1;

 while(SQLInstallerError(wErrNum,&pErr,szErrMsg,cbMsgBuffer,&cbRet)!=SQL_NO_DATA)
 {
  wErrNum++;
 };

 return (unsigned int)pErr;
}


void MkDate(TIMESTAMP_STRUCT& ts, TCHAR* szBuffer)
{
 TCHAR szMonth[4],szDay[4],szYear[8];

 _stprintf(szMonth,_T("%u"),ts.month);
 _stprintf(szDay,_T("%u"),ts.day);
 _stprintf(szYear,_T("%u"),ts.year);
 _tcscpy(szBuffer,szMonth);
 _tcscat(szBuffer,_T("/"));
 _tcscat(szBuffer,szDay);
 _tcscat(szBuffer,_T("/"));
 _tcscat(szBuffer,szYear);

 return;
}


TIMESTAMP_STRUCT ParseDate(TCHAR* szDate, TCHAR* szFormat, TCHAR* szDelimiter)
{
 UINT i=0,j=0,k=0;
 TIMESTAMP_STRUCT ts;
 TCHAR buf[3][8];             //buf[0] for month, buf[1] for day, buf[2] for year
 TCHAR* p;

 memset(buf,0,sizeof(buf));  //zero out buf[]
 p=szDate;
 for(i=0;i<_tcslen((TCHAR*)szDate);i++)
 {
     if(*p!=*szDelimiter)
     {
        buf[j][k++]=*p;
        buf[j][k+1]=_T('\0');
     }
     else
     {
        j++;
        k=0;
     }
     p++;
 }
 if(!_tcsicmp((TCHAR*)szFormat,_T("MDY")))
 {
    ts.month=(short)_ttoi(buf[0]);
    ts.day=(short)_ttoi(buf[1]);
    ts.year=(short)_ttoi(buf[2]);
 }
 if(!_tcsicmp((TCHAR*)szFormat,_T("DMY")))
 {
    ts.day=(short)_ttoi(buf[0]);
    ts.month=(short)_ttoi(buf[1]);
    ts.year=(short)_ttoi(buf[2]);
 }
 if(!_tcsicmp((TCHAR*)szFormat,_T("YMD")))
 {
    ts.year=(short)_ttoi(buf[0]);
    ts.month=(short)_ttoi(buf[1]);
    ts.day=(short)_ttoi(buf[2]);
 }

 return ts;
}